<template>
  <div class="tb-container">
    <el-card>
      <search-tool @onSearch="handleSearch"></search-tool>
      <div style="margin-bottom: 16px">
        <el-button @click="handleAdd" type="primary">新增申请</el-button>
        <el-button @click="handleCommit" type="primary">提交申请</el-button>
      </div>

      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        :cell-class-name="cellcb"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" v-if="isAudit === '1'" />
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column label="序号" type="index" width="70" />
        <el-table-column prop="projectName" label="工程名称" width="110">
          <template #default="{ row }">
            <span>{{ row.pipelineApplyForm.projectName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="管线类型" width="110">
          <template #default="{ row }">
            <span>{{ row.pipelineApplyForm.type }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="pipeId" label="所属管廊" width="110">
          <template #default="{ row }">
            <span>{{ row.pipeInfoName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="storehouseId" label="所属仓室" width="120">
          <template #default="{ row }">
            <span>{{ row.storehouseName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="subregionNames" label="防火分区" width="120">
          <template #default="{ row }">
            <span>{{ row.pipelineApplyForm.subregionNames }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="unit" label="申报单位" width="120">
          <template #default="{ row }">
            <span>{{ row.pipelineApplyForm.unit }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申报日期" width="120">
          <template #default="{ row }">
            <span>{{ row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="startWorkDate" label="开工日期" width="120">
          <template #default="{ row }">
            <span>{{ row.pipelineApplyForm.startWorkDate }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="endWorkDate" label="竣工日期" width="120">
          <template #default="{ row }">
            <span>{{ row.pipelineApplyForm.endWorkDate }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="dealDate" label="审核日期" width="140">
          <template #default="{ row }">
            <span>{{ row.dealDate }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="审核人" width="120">
          <template #default="{ row }">
            <span>{{ row.userName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="statusName" label="状态" width="160">
          <template #default="{ row }">
            <!-- <span class="state"></span> -->
            <span
              class="state"
              :style="
                row.statusName === '创建'
                  ? {
                      background: '#62c654',
                      'box-shadow': '0px 0px 6px 1px #62c654'
                    }
                  : row.statusName === '不通过'
                  ? {
                      background: '#FF5751',
                      'box-shadow': '0px 0px 6px 1px #FF5751'
                    }
                  : row.statusName === '已通过'
                  ? {
                      background: '#2962FF ',
                      'box-shadow': '0px 0px 6px 1px #2962FF '
                    }
                  : row.statusName === '已完工'
                  ? {
                      background: '#2962FF',
                      'box-shadow': '0px 0px 6px 1px #2962FF'
                    }
                  : row.statusName === '未开工'
                  ? {
                      background: '#F88724',
                      'box-shadow': '0px 0px 6px 1px #F88724'
                    }
                  : row.statusName === '延期'
                  ? {
                      background: '#F82B24',
                      'box-shadow': '0px 0px 6px 1px #F82B24'
                    }
                  : {
                      background: '#62c654',
                      'box-shadow': '0px 0px 6px 1px #62c654'
                    }
              "
            ></span
            >&nbsp;
            <span
              :style="
                row.statusName === '创建'
                  ? {
                      color: '#62c654'
                    }
                  : row.statusName === '不通过'
                  ? {
                      color: '#FF5751'
                    }
                  : row.statusName === '已通过'
                  ? {
                      color: '#2962FF '
                    }
                  : row.statusName === '已完工'
                  ? {
                      color: '#2962FF'
                    }
                  : row.statusName === '未开工'
                  ? {
                      color: '#F88724'
                    }
                  : row.statusName === '延期'
                  ? {
                      color: '#F82B24'
                    }
                  : {
                      color: '#62c654'
                    }
              "
              >{{ row.statusName }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140">
          <template #default="{ row }">
            <div class="action-row">
              <div>
                <el-tooltip content="删除" placement="top" effect="light">
                  <svg-icon
                    class="iconsty"
                    v-show="row.statusName === '创建'"
                    icon="acpp-del"
                    style="margin-right: 10px"
                    @click="handleDelete(row)"
                  ></svg-icon>
                </el-tooltip>
                <el-tooltip content="审批" placement="top" effect="light">
                  <svg-icon
                    class="iconsty"
                    icon="pipe-audit"
                    style="cursor: pointer"
                    v-show="
                      row.statusName !== '不通过' && row.statusName !== '已完工'
                    "
                    @click="handleAudit(row)"
                  ></svg-icon>
                </el-tooltip>
                <svg-icon
                  class="iconsty"
                  icon="pipe-unaudit"
                  style="cursor: not-allowed"
                  v-show="
                    row.statusName === '不通过' || row.statusName === '已完工'
                  "
                ></svg-icon>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        layout="->,prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <add-dialog
        :title="dialogTitle"
        :rowData="curRowData"
        v-model:visible="visibleAddDialog"
        @onReload="reloadPage"
      ></add-dialog>
      <audit-dialog
        :title="dialogTitle"
        :AuditData="AuditData"
        v-model:visible="visibleAuditDialog"
        @onReload="reloadPage"
      ></audit-dialog>
      <commit-dialog
        :title="dialogTitle"
        :rowListData="rowListData"
        v-model:visible="visiblecommitDialog"
        @onReload="reloadPage"
      ></commit-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import SearchTool from './components/SearchTool/index.vue'
import AddDialog from './components/AddDialog/index.vue'
import commitDialog from './components/commitDialog/index.vue'
import AuditDialog from './components/AuditDialog/index.vue'
import { ElMessageBox } from 'element-plus'
import { pipeLineApplyList, delpipeLineApply } from '@/api/pipeline'
const tableData = ref([])
const total = ref(0)
const page = ref(1)
const size = ref(8)
const params = ref({})
const loading = ref(false)
const dialogTitle = ref('')
const visibleAddDialog = ref(false)
const visiblecommitDialog = ref(false)
const visibleAuditDialog = ref(false)
const rowListData = ref({})
const AuditData = ref({})

// 获取列表数据
const getListData = async (params) => {
  loading.value = true
  const res = await pipeLineApplyList(params, page.value, size.value)
  loading.value = false
  tableData.value = res.rows
  total.value = res.total
}

const reloadPage = () => {
  getListData(params.value)
  if (
    visiblecommitDialog.value ||
    visibleAddDialog.value ||
    visibleAuditDialog.value
  ) {
    visiblecommitDialog.value = false
    visibleAddDialog.value = false
    visibleAuditDialog.value = false
  }
}
reloadPage()

// 控制当前复选框的显示隐藏
const cellcb = (row) => {
  // console.log(row.row.isAudit === '1')
  if (
    row.row.isAudit === '1'
    // ||
    // (row.row.statusName === '已完工' && row.row.statusName === '不通过')
  ) {
    return 'un_show'
  }
}
// const selectable = (row) => {
//   console.log(row)
//   if (row.status === 1) {
//     return true
//   } else {
//     return false
//   }
// }

// 复选框的函数
const handleSelectionChange = (val) => {
  rowListData.value = val
  console.log(val)
}

const handleSearch = (paramsData) => {
  params.value = paramsData
  getListData(paramsData)
}

// 分页相关
/**
 * size 改变触发
 */
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData(params.value)
}

/**
 * 页码改变触发
 */
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData(params.value)
}

// 增删改相关
const handleAdd = (row) => {
  dialogTitle.value = '新增申请'
  visibleAddDialog.value = true
}
const handleCommit = () => {
  dialogTitle.value = '提交申请'
  visiblecommitDialog.value = true
}
const handleAudit = (row) => {
  console.log(row.statusName)
  if (row.statusName === '创建' || row.statusName === '已通过') {
    dialogTitle.value = '审批'
  } else if (row.statusName === '未开工') {
    dialogTitle.value = '开工审批'
  } else {
    dialogTitle.value = '已完工'
  }
  AuditData.value = row
  visibleAuditDialog.value = true
}

const handleDelete = (
  row,
  done = async () => {
    console.log(row)
    // 删除
    await delpipeLineApply(row.pipelineApplyForm.id)
    reloadPage()
  }
) => {
  console.log(row)
  ElMessageBox.confirm('确定删除该条信息吗?', '删除信息', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
    .then(() => {
      done()
    })
    .catch(() => {
      // catch error
    })
}
</script>

<style lang="scss" scoped>
.tb-container {
  :deep(.el-table th.el-table__cell) {
    background-color: #e4edff;
  }
}
.state {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 10px;
  background: #62c654;
  box-shadow: 0px 0px 6px 1px #35b15c;
  opacity: 1;
}
// .un_show {
//   :deep(.el-checkbox__input) {
//     display: none;
//   }
// }
// .un_show .el-checkbox__input {
//   display: none;
// }

:deep(.el-table th.el-table__cell > .cell .el-checkbox__input) {
  display: none;
}
:deep(.svg-icon) {
  outline: 0;
}
</style>
